﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SalesCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SalesCommandHandler
{
    public class UpdateDeliveryNoticeCommandHandler : IRequestHandler<UpdateDeliveryNoticeCommand, APIResult<int>>
    {
        private readonly IBaseRepository<DeliveryNotice> deliverRep;
        private readonly IBaseRepository<OrderMaterials> materialBiz;
        public UpdateDeliveryNoticeCommandHandler(IBaseRepository<DeliveryNotice> deliverRep, IBaseRepository<OrderMaterials> materialBiz, IMapper mapper)
        {
            this.deliverRep = deliverRep;
            this.materialBiz = materialBiz;
        }
        public async Task<APIResult<int>> Handle(UpdateDeliveryNoticeCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var deliverlist = deliverRep.GetAll().Result.Where(x=>x.DocumentCode == request.DocumentCode).FirstOrDefault();
            deliverlist.DocumentDate = request.DocumentDate;
            deliverlist.BusinessTypeId = request.BusinessTypeId;
            deliverlist.WarehouseId = request.WarehouseId;
            deliverlist.Consignee = request.Consignee;
            deliverlist.Operator = request.Operator;
            deliverlist.CustomerName = request.CustomerName;
            deliverlist.Phone = request.Phone;
            deliverlist.DocumentStatus = request.DocumentStatus;
            deliverlist.Auditor = request.Auditor;
            deliverlist.AuditDate = request.AuditDate;
            deliverlist.DeliveryDate = request.DeliveryDate;
            deliverlist.BusinessStatusId = request.BusinessStatusId;
            deliverlist.FreightCost = request.FreightCost;
            deliverlist.HandlingFee = request.HandlingFee;
            deliverlist.Address = request.Address;
            deliverlist.Remark = request.Remark;
            var res= deliverRep.Update(deliverlist).Result;
            if (res > 0)
            {
                var res2 = materialBiz.GetAll().Result.Where(x => x.SaleCode == deliverlist.NoticeId).ToList();
                
                foreach (var item in request.OrderMaterials)
                {
                    var i = 0;
                    res2[i].SaleCode = item.SaleCode;
                    res2[i].MaterialsCode = item.MaterialsCode;
                    res2[i].MaterialsName = item.MaterialsName;
                    res2[i].ModelSpecification = item.ModelSpecification;
                    res2[i].Calulate = item.Calulate;
                    res2[i].CalulateNumber = item.CalulateNumber;
                    res2[i].PackRequir = item.PackRequir;
                    res2[i].Remark = item.Remark;
                    var res3 = materialBiz.Update(res2[i]).Result;
                    i += 1;
                }
                    result.Code = APIEnum.修改成功;
                    result.Msg = APIEnum.修改成功.ToString();
                    result.data = res;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
                result.data = res;
            }
            return await  Task.FromResult(result);
        }
    }
}
